<template>
	<view>
		<mc-framework navTitle="MES工控机" @changeLevel="changeLevel">

			<template #content>
				<view class="content" style="height: 100%;">
					<view class="contentBox">
						<view class="container">
							<uni-forms ref="baseForm" :modelValue="baseFormData">
								<view class="form-item-row">
									<view class="custom-cell">申请人填写</view>

									<uni-forms-item label="模具:" label-width="auto" required>

										<view class="uni_forms_item_view" @click="openBaseScreen('模具')">
											<text v-if="!baseFormData.MoludName"
												class="u_text">{{FormData.MoludName}}</text>
											<text v-else>{{baseFormData.MoludName}}</text>
										</view>
									</uni-forms-item>
									<uni-forms-item label="申请人:" label-width="auto">
										<input v-model="baseFormData.ApplyForMan" type="text" disabled />
									</uni-forms-item>
									<uni-forms-item label="申请时间:" label-width="auto">
										<input v-model="baseFormData.ApplyForTime" type="text" disabled />
									</uni-forms-item>
									<uni-forms-item label="故障描述:" label-width="auto" style="width: 95.5vw;" required>
										<input v-model="baseFormData.BXContent" type="text" placeholder="请输入故障描述"
											style="width: 85vw;" />
									</uni-forms-item>

									<view class="custom-cell">维修人填写</view>

									<uni-forms-item label="维修编号:" label-width="auto">
										<input v-model="baseFormData.BillNO" type="text" placeholder="自动带出维修编号"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="维修人:" label-width="auto" required>
										<input v-model="baseFormData.MaintainMan" type="text" disabled
											placeholder="请选择维修人" />
										<text class="text" @click="openBaseScreen('人员')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修内容:" label-width="auto" required>
										<input v-model="baseFormData.GZCause" type="text" placeholder="请输入维修内容" />
									</uni-forms-item>
									<uni-forms-item label="维修方式:" label-width="auto" required>
										<input v-model="baseFormData.WayName" type="text" placeholder="请选择维修方式"
											disabled />
										<text class="text" @click="addWay('WXFS')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修费用:" label-width="auto" required>
										<input v-model="baseFormData.Cost" type="text" placeholder="请输入维修费用" />
									</uni-forms-item>

									<uni-forms-item label="维修状态:" label-width="auto">
										<input v-model="baseFormData.WCStatus?'已维修':'未维修'" type="text"
											placeholder="维修状态" disabled />
									</uni-forms-item>
									<uni-forms-item label="维修结果:" label-width="auto" required>
										<input v-model="baseFormData.FResultName" type="text" placeholder="请选择维修结果"
											disabled />
										<text class="text" @click="addWay('SBWX')"></text>
									</uni-forms-item>
									<uni-forms-item label="维修开始时间:" label-width="auto">
										<input v-model="baseFormData.BeginTime" type="text" placeholder="自动带出开始时间"
											disabled />
									</uni-forms-item>
									<uni-forms-item label="维修结束时间:" label-width="auto">
										<input v-model="baseFormData.WCTime" type="text" placeholder="自动带出结束时间"
											disabled />
									</uni-forms-item>
								</view>

							</uni-forms>
						</view>
					</view>
				</view>
				<view class="bottomBtnsOne">
					<button block @click="submit" v-show="buttonShow">
						{{buttonTitle}}
					</button>
				</view>
				<u-action-sheet :actions="StateList" :show="StateShow" @select="StateClick"
					@close="StateShow=false"></u-action-sheet>
				<u-toast ref="uToast"></u-toast>
				<u-popup :show="popup_logout" mode="center" :closeOnClickOverlay="false" :round="4">
					<view class="popup-content">
						<view class="popup-content_close x-c">
							<uni-icons type="closeempty" color="#999" size="20"
								@click='popup_logout = false'></uni-icons>
						</view>
						<view class="x-c title">
							<text>{{popuptitle}}</text>
						</view>
						<view class="contentView">
							<text>{{popupcontent}}</text>
						</view>
						<view class="btnBox x-ac">
							<u-button type="primary" text="确定" @click="confirmClick"></u-button>
						</view>
					</view>
				</u-popup>
			</template>
		</mc-framework>
	</view>
</template>

<script>
	import {
		getTimeStamp,
		getFormatDateTime,
		getBefore7DateTime
	} from '@/mes/common/base.js';
	export default {
		data() {
			return {
				popup_logout: false,
				popuptitle: '',
				popupcontent: '',
				FormData: {
					MoludName: '请选择模具名称',
					EquipmentName: '请选择机台'
				},
				baseFormData: {
					EquipmentName: '',
					ApplyForMan: '',
					ApplyForTime: '',
					BXContent: '',
					BillNO: '',
					MaintainMan: '',
					GZCause: '',
					Way: '',
					WayName: '',
					Cost: '',
					WCStatus: '',
					FResult: 0,
					FResultName: '',
					BeginTime: '',
					WCTime: '',
					BillDate: '',
					MoludID: '',
					MoludName: ''
				},
				buttonShow: true,
				buttonTitle: '保存',

				StateShow: false,
				StateList: [],
				moduleShow: false,
				moduleList: []
			};
		},
		onLoad(item) {
			var that = this
			console.log(item);
			if (item.MaintainMouldID) {
				that.queryList(item.MaintainMouldID)
			} else {
				if (item.item) {
					var item = JSON.parse(item.item)
					console.log(item);
					this.baseFormData = item
					this.baseFormData.BXContent = this.baseFormData.BXContent = item.FaultName + ':' + item.GZContent
				}
				this.baseFormData.ApplyForMan = this.baseFormData.ApplyForMan ? this.baseFormData.ApplyForMan : this
					.$store.state.UserInfo.UserName
				this.baseFormData.CheckupID = this.baseFormData.CheckupID ? this.baseFormData.CheckupID : this.$store
					.state.UserInfo.EmplID
				this.baseFormData.ApplyForTime = this.baseFormData.ApplyForTime ? this.baseFormData.ApplyForTime :
					getFormatDateTime(new Date())
				this.baseFormData.BillDate = this.baseFormData.BillDate ? this.baseFormData.BillDate :
					getFormatDateTime(new Date())
			}
		},
		onShow() {
			this.getValue
		},
		onBackPress(options) { // 监听页面返回
			if (options.from == 'navigateBack') { // 通过点击路由返回
				return false;
			} else if (options.from == 'backbutton') { // 通过外置返回
				return true;
			}
		},
		methods: {
			confirmClick() {
				this.popup_logout = false
			},
			addPeriodTypeClick() {
				var that = this
				if (!that.baseFormData.BeginTime) {
					that.buttonTitle = '开始维修'
				} else if (that.baseFormData.BeginTime) {
					that.buttonTitle = '结束维修'
				}
				if (that.baseFormData.MaintainMouldID && that.baseFormData.BeginTime && that.baseFormData
					.WCTime) {
					that.buttonShow = false
				}
				that.$forceUpdate()
			},

			StateClick(e) {
				var that = this;
				console.log(e);
				console.log(that.StateName);
				if (that.StateName == 'SBWX') {
					console.log(e.id);
					that.baseFormData.FResult = e.id
					that.baseFormData.FResultName = e.name
					that.$forceUpdate()
				} else {
					console.log(that.StateName);
					that.baseFormData.WayName = e.name
					that.baseFormData.Way = e.id
					that.$forceUpdate()
				}
				this.$forceUpdate()
			},
			addWay(str) {
				var that = this;
				that.StateName = str
				console.log(str);
				that.Request.request('getViewState', {
					FType: str
				}, function(ret) {
					console.log(ret.data.ds);
					if (ret.status == 200) {
						that.StateList = ret.data.ds
					}
				})
				this.StateShow = true
			},


			getValue(e) {
				var that = this;
				if (e != '返回') {
					if (e.type == '人员') {
						that.baseFormData.MaintainMan = e.name

					} else if (e.type == '模具') {
						that.baseFormData.MoludID = e.MoludID
						that.baseFormData.MoludName = e.MoludCode
					}
					that.$forceUpdate()
				}
			},
			openBaseScreen(str) {
				var that = this;
				var obj;
				if (str == '人员') {
					obj = {

						type: str,
						cmd: 'getEmplByJobName',
						param: {
							JobName: '',
							PageIndex: 1,
							PageSize: 30
						}
					}
				} else if (str == '模具') {
					obj = {

						type: str,
						cmd: 'Droplist_Molud',
						param: {
							FWorkshopID: '1009',
							MoludCode: '',
							PageIndex: 1,
							PageSize: 30
						}
					}
				}
				uni.navigateTo({
					url: `../common/baseScreen?obj=${JSON.stringify(obj)}`,
					animationType: 'slide-in-right',
					animationDuration: 300
				});
			},



			submit() {
				var that = this
				var show = true
				var msg
				if (that.baseFormData.BXContent == '' || that.baseFormData.BXContent == null) {
					msg = '请输入故障描述'
					show = false
				}
				if (that.baseFormData.MoludID == '' || that.baseFormData.MoludID == null) {
					msg = '请选择模具'
					show = false
				}
				if (that.buttonTitle == '开始维修') {
					if (that.baseFormData.MaintainMan == '' || that.baseFormData.MaintainMan == null) {
						msg = '请选择人员'
						show = false
					} else if (that.baseFormData.GZCause == '' || that.baseFormData.GZCause == null) {
						msg = '请输入维修内容'
						show = false
					} else if (that.baseFormData.WayName == '' || that.baseFormData.WayName == null) {
						msg = '请选择维修方式'
						show = false
					} else if (that.baseFormData.Cost == '' || that.baseFormData.Cost == null) {
						msg = '请输入维修费用'
						show = false
					}
				} else if (that.buttonTitle == '结束维修') {
					if (that.baseFormData.FResultName == '' || that.baseFormData.FResultName == null) {
						msg = '请选择维修结果'
						show = false
					}
				}

				if (show) {
					var obj = {
						MaintainMouldID: that.baseFormData.MaintainMouldID ? that.baseFormData
							.MaintainMouldID : 0,
						ApplyForMan: that.baseFormData.ApplyForMan,
						MoludID: that.baseFormData.MoludID,
						ApplyForTime: that.baseFormData.ApplyForTime,
						BXContent: that.baseFormData.BXContent,
						BillNO: that.baseFormData.BillNO,
						MaintainMan: that.baseFormData.MaintainMan,
						GZCause: that.baseFormData.GZCause,
						Way: that.baseFormData.Way,
						Cost: that.baseFormData.Cost ? that.baseFormData.Cost : 0,
						WCStatus: that.baseFormData.WCStatus,
						FResult: that.baseFormData.FResult,
						BeginTime: that.baseFormData.BeginTime,
						WCTime: that.baseFormData.WCTime,
						BillDate: that.baseFormData.BillDate,
						FType: 'MJWX',
					}
					console.log(obj);
					that.Request.request('saveMaintainMould', obj, function(ret) {
						console.log(ret);
						if (ret.status == 200) {
							that.$refs.uToast.show({
								title: '保存',
								type: 'success',
								message: '保存成功'
							})
							that.queryList(ret.data.ds[0].MaintainMouldID)
						}
					})
				} else {
					that.popuptitle = '报错'
					that.popupcontent = msg
					that.popup_logout = true
				}
			},
			queryList(id) {
				var that = this
				try {
					var obj = {
						PageIndex: 1,
						PageSize: 30,
						FBDate: getBefore7DateTime(),
						FEDate: getFormatDateTime(new Date()),
						WCStatus: -1,
						MaintainMouldID: id
					}

					that.Request.request('getMaintainMouldListD', obj, (ret) => {
						console.log(ret.data);
						ret.data.ds.map((item) => {
							item.ApplyForTime = item.ApplyForTime ? item.ApplyForTime.split('T')[0] :
								''
							item.BeginTime = item.BeginTime ? item.BeginTime.replace('T',
								' ') : ''
							item.WCTime = item.WCTime ? item.WCTime.replace('T',
								' ') : ''
						})
						that.baseFormData = ret.data.ds[0]


						that.addPeriodTypeClick()
						that.$forceUpdate()

					})
				} catch (e) {
					console.log(e)
					this.$utils.hideLoading();
					this.$utils.msg('请求失败请重试')
				}
			},


			changeLevel(data, row) {
				let routes = getCurrentPages(); //当前页面栈
				let backIndex = routes.length - row.length; // 计算返回页面栈数
				if (backIndex == 1) {
					let prevPage = routes[routes.length - 2]
					prevPage.$vm.getList()
				}
				uni.navigateBack({
					delta: backIndex
				})
			},
		}
	}
</script>

<style lang="scss">
	@import "../css/index.css";

	.custom-cell {
		padding: 0.5% 1.5% !important;
		width: 100vw;
		font-size: 14px;
		font-weight: bold !important;
	}


	.buttonView {
		float: right;
		padding: 1%;

		.buttonstyle {
			color: #fff;
			background-color: #51d0d0;
		}
	}
</style>